function Blogger(){
    this.init = function(){
        $("a.player").each(function(i, obj){
			var url = $(obj).attr("href")
            if (url.indexOf("tube8") != -1) {
            	tube8Video(obj);
            }else if(url.indexOf("eporner") != -1){
            	epornerVideo(obj);
            }else {
                createFlowPlayer(obj);
            }
        }).click(function(){
            return false;
        });
        addDownloadImage();
    }
    
    var tube8Video = function(obj){
		$obj = $(obj);
        $.ajax({
            url: $obj.attr("href"),
            type: 'GET',
            success: function(res){
                var html = res.responseText;
                var v = html.search(/videourl/i);
                var first = html.indexOf('"', v) + 1;
                var last = html.indexOf('"', first);
                var url = html.substring(first, last);
                $obj.attr("href", url);
                createFlowPlayer(obj);
            }
        });
    }
    
    var epornerVideo = function(obj){
    	$obj = $(obj);
    	$.ajax({
            url: $obj.attr("href"),
            type: 'GET',
            success: function(res){
                var html = res.responseText;
                var $a = $(html).find("#mfdown > a");
                $a.each(function(i, a){
                	var url = $(a).attr("href");
                	if ($(a).attr("href").indexOf("dload") != -1){
                		$obj.attr("href", url.replace("dload", "1.flv"));
        				createFlowPlayer(obj);
                	}
                });
            }
        });
    }
    
    var createFlowPlayer = function(obj){
        flowplayer(obj, "http://avtubee.googlecode.com/svn/trunk/file/swf/flowplayer/flowplayer-3.2.2.swf", {
            plugins: {
                pseudo: {
                    url: 'http://avtubee.googlecode.com/svn/trunk/file/swf/flowplayer/flowplayer.pseudostreaming-3.2.2.swf'
                }
            },
            clip: {
                provider: 'pseudo',
                autoPlay: false,
                autoBuffering: true
            }
        });
    }
    
    var addDownloadImage = function(){
    	$("a.pc-download").append($("<img>").attr({
    		src: 'http://1.bp.blogspot.com/_ljOcyMVFbJM/TFmDhU4nnhI/AAAAAAAAAAc/UEWxVtt9YDc/s1600/download-icon-pc.png'
    	}));
    	$("a.mobile-download").append($("<img>").attr({
    		src: 'http://2.bp.blogspot.com/_ljOcyMVFbJM/TFl-iC7-mII/AAAAAAAAAAY/bFH1MKn4QXs/s1600/download-icon-mobile.png'
    	}));
    }
}

$(new Blogger().init);